package ru.ksu.ktk.diploma.sdi.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XPathQueryService;

import ru.ksu.ktk.diploma.sdi.dao.model.ResourceModel;
import ru.ksu.ktk.diploma.sdi.dao.util.Util;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class ResourcesDaoImpl implements ResourcesDao {

    protected static final Log log = LogFactory.getLog(ResourcesDaoImpl.class);

    private String driverName;

    enum ResourceType {
        XMLResource("XMLResource"), BinaryResource("BinaryResource");

        private String name;

        private ResourceType(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see ru.ksu.ktk.diploma.sdi.dao.ResourcesDao#get(java.lang.String)
     */
    public OntModel get(String uri) {
        if (uri == null || uri.length() == 0) throw new IllegalArgumentException("uri of resource cannot be null");

        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_LITE_MEM);
        ontModel.read(uri, "RDF/XML-ABBREV");
        return ontModel;
    }

    /**
     * @see ru.ksu.ktk.diploma.sdi.dao.ResourcesDao#save(String, File)
     */
    public void save(String uri, String resourceName, File file) {
        try{
            Collection collection = DatabaseManager.getCollection(uri);
            Resource resource = collection.createResource(resourceName, ResourceType.XMLResource.toString());
            resource.setContent(file);
            collection.storeResource(resource);
        }
        catch (XMLDBException e){
            log.error(e);
            throw new RuntimeException(e);
        }

    }

    public List<ResourceModel> getList(String uri) {
        List<ResourceModel> resourceModels = new ArrayList<ResourceModel>();
        try{
            Collection collection = DatabaseManager.getCollection(uri);
            String[] resourceNames = collection.listResources();
            for (String name : resourceNames){
                Resource resource = collection.getResource(name);
                String resourceId = resource.getId();
                boolean isXmlResource =
                    resource.getResourceType().equals(ResourceType.XMLResource.toString()) && Util.hasXmlExtension(resourceId);
                if (isXmlResource){
                    resourceModels.add(new ResourceModel(uri, resourceId));
                }
            }
            return resourceModels;
        }
        catch (XMLDBException e){
            log.error(e);
            throw new RuntimeException(e);
        }
    }

    public void init() throws Exception {
        @SuppressWarnings("unchecked")
        Class clazz = Class.forName(driverName);
        Database database = (Database) clazz.newInstance();
        DatabaseManager.registerDatabase(database);
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }
}
